### --start_docs

## Validate the undercloud installation
## ====================================
script_return_value=0

## * Install deps for subunit results
sudo {{ ansible_pkg_mgr }} install -y python-os-testr

## * Set the uri of TripleO UI based on SSL usage
## ::
{% set ui_uri = "https://%s" % undercloud_network_cidr|nthhost(2)  if undercloud_generate_service_certificate|bool else "http://%s:3000" % undercloud_network_cidr|nthhost(1) %}

## * Specify the function to test UI
## ::
function ui_sanity_check {
    if [ -f "/etc/httpd/conf.d/25-tripleo-ui.conf" ]; then
        if ! curl {{ ui_uri }} 2>/dev/null | grep -q 'TripleO'; then
            echo "ERROR: TripleO UI front page is not loading."
            generate-subunit $(date +%s) 10 fail tripleo_ui_test >> {{ working_dir }}/undercloud_sanity.subunit
            script_return_value=1
        fi
        generate-subunit $(date +%s) 10 success tripleo_ui_test >> {{ working_dir }}/undercloud_sanity.subunit
    fi
}

# * Specify the function to test CLI
## ::
function citest {
    test_name=`sed 's/ /_/g' <<< "$@"`
    "$@"
    local status=$?
    if [ $status -ne 0 ]; then
        echo "error with $1" >&2
        generate-subunit $(date +%s) 10 fail $test_name >> {{ working_dir }}/undercloud_sanity.subunit
        script_return_value=1
    else
        generate-subunit $(date +%s) 10 success $test_name >> {{ working_dir }}/undercloud_sanity.subunit
    fi
    return $status
}



## * Check the answers from each undercloud service
## ::

set -x
rm -f {{ working_dir }}/undercloud_sanity.subunit.gz
rm -f {{ working_dir }}/undercloud_sanity.subunit

source {{ working_dir}}/stackrc
{% if undercloud_enable_nova|bool %}
citest openstack compute service list
citest openstack image list
{% endif %}
citest openstack subnet list
citest openstack network list
citest openstack network agent list
citest openstack baremetal node list
{% if undercloud_enable_heat|bool %}
citest openstack stack list
{% endif %}
ui_sanity_check

exit $script_return_value

set +x

### --stop_docs
